package com.exp.exp03.controller;

import com.exp.exp03.pojo.RegionStats;
import com.exp.exp03.service.MyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class MyController {
    final MyService getDataService;

    public MyController(MyService getDataService) {
        this.getDataService = getDataService;
    }

    @GetMapping("/")
    public String index(Model model) {
        List<RegionStats> list=getDataService.getStatList();
        model.addAttribute("statList", list);
        model.addAttribute("countryOrRegionList", getDataService.getCountryOrRegionList(getDataService.getStatList()));
        return "index";
    }

    @RequestMapping(value = "country")
    public String searchCountry(@RequestParam(required = false) String countryOrRegion, Model model) {
        List<RegionStats> list=getDataService.getStatListByCountryOrRegion(countryOrRegion, getDataService.getStatList());
        model.addAttribute("countryOrRegionList", getDataService.getCountryOrRegionList(getDataService.getStatList()));
        model.addAttribute("statList", list);
        return "index";
    }
}
